// pages/evaluation/index.js
let app = getApp()
import {
  isEmpty
} from '../../utils/util.js';
import Http from '../../utils/http.js';

function transLevel(value) {
  switch (value) {
    case 1:
      return 'ONE';
    case 2:
      return 'TWO';
    case 3:
      return 'THREE';
    case 4:
      return 'FOUR';
    case 5:
      return 'FIVE';
  }
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    evaImgList: [], //评论图
    orderCode: '',
    commodityInfo: {},
    anonymous: 'NO_ANONYMOUS',
    wholeSatisfactionDegree: 5, //整体满意度
    serviceSatisfactionDegree: 5, //服务态度
    descriptionSatisfactionDegree: 5, //服务态度
    commentContent: '',
    submitWaiting: false,
    comtype:'',//商品类型
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      orderCode: options.orderCode,
      commodityInfo: JSON.parse(options.commodityInfo),
      comtype: options.type
    })
  },
  degreeChange:function(e){
    const value = e.detail.value;
    const dataset = e.currentTarget.dataset.rate
    this.setData({
      [dataset]:value
    })
  },
  bindChooiceProduct: function() {
    var that = this;
    wx.chooseImage({
      count: 3, //最多可以选择的图片总数  
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有  
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有  
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片  
        var tempFilePaths = res.tempFilePaths;
        //启动上传等待中...  
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 10000
        })
        var uploadImgCount = 0;
        for (var i = 0, h = tempFilePaths.length; i < h; i++) {
          console.log(tempFilePaths[i])
          wx.uploadFile({
            url: app.globalData.domain + '/api/customer/order/upload/single',
            filePath: tempFilePaths[i],
            name: 'file',
            formData: {
              'imgIndex': i
            },
            header: {
              "Content-Type": "multipart/form-data"
            },
            success: function(res) {
              uploadImgCount++;
              var data = JSON.parse(res.data);
              if(data.code === 200){
                let evaImgList = that.data.evaImgList;
                evaImgList.push(data.data.url)
                //如果是最后一张,则隐藏等待中  
                if (uploadImgCount == tempFilePaths.length) {
                  wx.hideToast();
                  that.setData({
                    evaImgList
                  })
                }
              }else{
                wx.hideToast();
                wx.showModal({
                  title: '错误提示',
                  content: '上传图片失败,' + data.msg,
                  showCancel: false,
                  success: function (res) { }
                })
                return
              }

            },
            fail: function(res) {
              wx.hideToast();
              wx.showModal({
                title: '错误提示',
                content: '上传图片失败',
                showCancel: false,
                success: function(res) {}
              })
            }
          });
        }
      }
    });
  },
  //删除图片
  delImg(e) {
    const index = e.currentTarget.dataset.index;
    let evaImgList = this.data.evaImgList;
    evaImgList.splice(index, 1);
    this.setData({
      evaImgList
    })
  },
  selectClick() {
    const anonymous = this.data.anonymous === 'ANONYMOUS' ? 'NO_ANONYMOUS' : 'ANONYMOUS'
    this.setData({
      anonymous
    })
  },
  //发表评价
  handleSubmit() {
    if (this.data.submitWaiting) return
    if (isEmpty(this.data.commentContent)) {
      wx.showToast({
        title: '请填写评价',
        icon: "none",
        duration: app.globalData.duration
      })
      return
    }
    this.setData({
      submitWaiting: true
    })
    const data = {
      "anonymous": this.data.anonymous,
      "commentContent": this.data.commentContent,
      "commentImage": this.data.evaImgList,
      "descriptionSatisfactionDegree": transLevel(this.data.descriptionSatisfactionDegree),
      "serviceSatisfactionDegree": transLevel(this.data.serviceSatisfactionDegree),
      "wholeSatisfactionDegree": transLevel(this.data.wholeSatisfactionDegree)
    }
    Http.Request('POST', `/api/customer/order/comment/${this.data.orderCode}/v1`, data).then(res => {
      wx.showToast({
        title: '感谢您的评价',
        icon: "success",
        duration: app.globalData.duration
      })
      this.setData({
        submitWaiting: false
      })
      setTimeout(()=>{
        switch (this.data.comtype) {
          case 'ROOM_COMMODITY':
            wx.redirectTo({
              url: `../houseList/houseDetail/createOrder/index?orderCode=${this.data.orderCode}`,
            })
            break;
          case 'VIRTUAL_COMMODITY':
            wx.redirectTo({
              url: `../travelList/travelDetail/createOrder/index?orderCode=${this.data.orderCode}`,
            })
            break;
          case 'MATERIAL_COMMODITY':
            wx.redirectTo({
              url: `../shiwuList/shiwuDetail/createOrder/index?orderCode=${this.data.orderCode}`,
            })
            break;
          case 'CAR_COMMODITY':
            wx.redirectTo({
              url: `../carList/carDetail/createOrder/index?orderCode=${this.data.orderCode}`,
            })
            break;
        }
      }, app.globalData.duration)
    }).catch(err => {
      this.setData({
        submitWaiting: false
      })
      wx.showToast({
        title: err,
        icon: "none",
        duration: app.globalData.duration
      })
    })
  },
  bindTextAreaBlur(e) {
    const value = e.detail.value;
    this.setData({
      commentContent: value
    })
  },
  onShareAppMessage() {
    app.globalFn.onShareAppMessage()
  }
})